#include <Color.h>
#include <ColorConvert.h>

#define REDPIN 3
#define GREENPIN 6
#define BLUEPIN 5

boolean modulationDirection = true;
boolean doModulate = true;
int modulationTime = 2000;
int modulationCounter = 0;
int maxValue = 1000;

void setup() {
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
}

void loop() {
    RGB color;
    color.r = 80;
    color.g = 30;
    color.b = 80;
    if (doModulate) {
      color = modulate(color);
    }
    sendColorToPin(REDPIN, color.r);
    sendColorToPin(GREENPIN, color.g);
    sendColorToPin(BLUEPIN, color.b);
    //delay(500);
    //doModulate = !doModulate;
}

void sendColorToPin(int pin, int color) {
  int value = constrain(color, 0, 255);
  analogWrite(pin, value);
}

RGB modulate(RGB color) {
  HSV hsvColor = RGBtoHSV(color);

  if (modulationCounter >= modulationTime) {
    modulationCounter = 0;
    if (modulationDirection) {
      modulationDirection = false;
    } else {
      modulationDirection = true;
    }
  }
  
  float modulationAmount = (1.0 / modulationTime) * modulationCounter;
  
  if (modulationDirection) {
    hsvColor.v = modulationAmount;
  } else {
    hsvColor.v = 1 - modulationAmount;
  }  
  modulationCounter++;
 
  color = HSVtoRGB(hsvColor);   
  return color;  

}
